import { Record, List } from 'immutable';
import generateId from '../../utils/generate-id';
import Field from './Field';

const DEFAULTS = {
  id: undefined,
  name: undefined,
  fields: List()
};

class CustomType extends Record(DEFAULTS) {
  static isCustomType(object) {
    return !!object.__dataType__;
  }

  static create(attr = {}) {
    if (CustomType.isCustomType(attr)) {
      return attr;
    }
    const { name, fields = [], id = generateId() } = attr;
    return new CustomType({ name, fields: Field.createList(fields), id });
  }
}

CustomType.prototype.__dataType__ = true;
export default CustomType;
